//
//  YSUpdateTipsView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/3/1.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSUpdateTipsView.h"
#import "CustomShowView.h"

@interface YSUpdateTipsView()
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewHeightCons;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation YSUpdateTipsView

+ (instancetype)tipsView{
    return [[[NSBundle mainBundle] loadNibNamed:@"YSUpdateTipsView" owner:nil options:nil] lastObject];
}

- (IBAction)cancel:(UIButton *)sender {
    switch (sender.tag) {
        case 1:
            if (_appInfo && [_appInfo.appVersion compare:[SystemUtil getAppVersion]]==NSOrderedDescending){
                NSString *url = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=";
                url = [url stringByAppendingString:kAppleId];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
                
                [TalkingData trackEvent:@"HomeUpgradeYes"];
            }
            break;
        case 0:
            if (_appInfo && ![NSString isBlankString:_appInfo.appVersion]) {
                [DefaultsHelper setObject:_appInfo.appVersion forKey:kSkipUpgradeVersion];
                [TalkingData trackEvent:@"HomeUpgradeCancel"];
            }
            break;
        default:
            break;
    }
            
    CustomShowView *superView = (CustomShowView *) self.superview;
    [superView hideWithAnim:YES];
}

- (void)setAppInfo:(YSSetupInfoRes *)appInfo{
    _appInfo = appInfo;

    NSString *text = appInfo.appContent;

    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    self.contentLabel.attributedText = attributedString;
    
    [self layoutIfNeeded];
    CGFloat height = self.contentLabel.frame.size.height;
    self.scrollView.scrollEnabled = NO;
    if (height >= 150) {
        self.scrollView.scrollEnabled = YES;
        height = 150;
    }
    self.scrollViewHeightCons.constant = height + self.contentLabel.frame.origin.x + self.contentLabel.frame.origin.x;

}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGRect rect = self.bounds;
    rect.size.height = CGRectGetMaxY(self.containView.frame);
    self.bounds = rect;
    
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >=7 && version <8) {
        [super layoutSubviews];
    }
}

@end
